package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.entity.AddressBook;
import com.reggie.entity.Orders;
import com.reggie.entity.ShoppingCart;
import com.reggie.service.AddressBookService;
import com.reggie.service.OrderDetailService;
import com.reggie.service.OrderService;
import com.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单明细
 * @program: reggie
 * @author:
 * @create: 2022-10-06 17:12
 **/
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    /**
     * 分页查询
     * 根据订单号进行精确查询
     * */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String number){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(number!=null,Orders::getNumber,number);
        orderService.page(ordersPage,wrapper);
        return R.success(ordersPage);
    }

    /**
     * 提交订单信息
     * addressBookId:"1580121187848110082"
     * payMethod:1
     * remark:""
     * */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders, HttpSession session){

        Long userId = (Long) session.getAttribute("user");
        orders.setUserId(userId);
        orderService.submit(orders,userId);


        return R.success("提交成功");

    }

    /**
     * 查看订单的信息
     * */
    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize,HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId,userId);
        orderService.page(ordersPage,wrapper);
        return R.success(ordersPage);

    }
}
